package poc.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    public static String getDirFromPackage(String packageName) {
        return packageName.replace(".", "/");
    }

    public static String getFirstLetterUpperCaseString(String value) {
        if(value!=null&&!value.equals("")){
            return value.substring(0, 1).toUpperCase() + value.substring(1);
        }else{
            return "";
        }
    }

    public static String getFirstLetterLowerCaseString(String value) {
        if(value!=null&&!value.equals("")){
            return value.substring(0, 1).toLowerCase() + value.substring(1);
        }else{
            return "";
        }
    }

    public static String getUnderlineNameToCamel(String underlineName) {
        String[] temp = underlineName.split("_");
        String camelName = "";
        for (int i = 0; i < temp.length; i++) {
            camelName += getFirstLetterUpperCaseString(temp[i].toLowerCase());
        }
        return camelName;
    }

    public static String getCamelNameToUnderLine(String camelName) {
        char[] chararr=camelName.toCharArray();
        StringBuilder underLineNameStringBuilder=new StringBuilder();
        for(int i=0;i<chararr.length;i++)
        {
            underLineNameStringBuilder.append(chararr[i]);
            if((i+1<chararr.length-1)&&Character.isUpperCase(chararr[i+1]))
            {
                underLineNameStringBuilder.append("_");
            }
        }
        return underLineNameStringBuilder.toString().toLowerCase();
    }

    public static List<String> getMatchStrings(String regularExpression, String origin) {
        List<String> result = new ArrayList<String>();
        Pattern p = Pattern.compile(regularExpression);
        Matcher m = p.matcher(origin);

        while (m.find()) {
            result.add(m.group());
        }
        return result;
    }

    //groupIndex =0：匹配的是自己 1：匹配第一个括号内的内容
    public static List<String> getMatchGroupStrings(String regularExpression, String origin,int groupIndex) {
        List<String> result = new ArrayList<String>();
        Pattern p = Pattern.compile(regularExpression);
        Matcher m = p.matcher(origin);
        if(groupIndex>m.groupCount())
        {
            throw new RuntimeException("groupIndex=="+groupIndex+" 必须小于等于 matcher.groupCount()=="+m.groupCount()+"请检查正则表达式 "+ regularExpression +" 中匹配的group组");
        }

        while (m.find()) {
            result.add(m.group(groupIndex));
        }
        return result;
    }

    public static boolean hasMatchStrings(String regularExpression, String origin) {
        List<String> result = getMatchStrings(regularExpression,origin);
        return result.size()>0;
    }

    public static List<Integer> getMatchStringsPositions(String regularExpression, String origin) {
        List<Integer> result = new ArrayList<Integer>();
        Pattern p = Pattern.compile(regularExpression);
        Matcher m = p.matcher(origin);

        while (m.find()) {
            result.add(m.start());
        }
        return result;
    }

    public static String encodeBase64String(String origin){
        try {
            String base64Str = (new BASE64Encoder()).encode(origin.getBytes("utf-8"));
            return base64Str;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static String encodeBytes(byte [] data){
        try {
            String base64Str = (new BASE64Encoder()).encode(data);
            return base64Str;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static String decoderBase64File(String base64Code) {
        try {
            byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
            String result=new String(buffer,"utf-8");
            return result;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static String decoderBase64FileByGBK(String base64Code) {
        try {
            byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
            String result=new String(buffer,"GBK");
            return result;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static boolean containsTag(String originalString,String regularExpression){
        return hasMatchStrings(regularExpression,originalString);
    }

    public static boolean isEmpty(String string){
        return string == null || string.length() == 0;
    }

}
